#! /usr/bin/env python
# -*- coding: utf-8 -*-

import requests
import re
import random


class CollectProxy(object):
    '''Get proxy from http://www.xicidaili.com'''

    url = 'http://www.xicidaili.com/nn/'
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64)", "Referer": "http://www.xicidaili.com/nn", }

    def do(self, pages=1):
        proxies = set()
        pattern = re.compile(r'<td>(\d+\.\d+\.\d+\.\d+)</td>\s*?<td>(\d{1,5})</td>')

        for page in range(1, pages+1):
            url = self.url + str(page)
            # print(url)
            response = requests.get(url, headers=self.headers)
            for item in re.findall(pattern, response.text):
                # print(item[0] + ":" + item[1])
                proxies.add(item[0] + ':' + item[1])

        return proxies

    def check(self, proxy, target='https://www.baidu.com'):
        proxies = {'http': 'http://' + proxy, 'https': 'https://' + proxy}
        headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64)"}
        try:
            r = requests.get(target, headers=headers, proxies=proxies, timeout=3)
            if r.status_code == 200:
                return True
            else:
                return False
        except:
            return False


class ProxyPool(object):
    '''Proxy Pool'''

    cp = CollectProxy()

    def __init__(self):
        self.pool = set()

    def updateCP(self, pages=1):
        self.pool.update(self.cp.do(pages=pages))

    def removeProxy(self, proxy):
        if proxy in self.pool:
            self.pool.remove(proxy)

    def randomChoose(self):
        if not self.pool:
            self.updateCP(random.randint(1, 5))
        return random.sample(self.pool, 1)[0]

    def getProxy(self, target='https://www.baidu.com'):
        proxy = self.randomChoose()
        proxies = {'http': 'http://' + proxy, 'https': 'https://' + proxy}
        headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64)"}
        try:
            r = requests.get(target, headers=headers, proxies=proxies, timeout=3)
            if r.status_code == 200:
                return proxy
            else:
                self.removeProxy(proxy)
                return self.getProxy()
        except:
            self.removeProxy(proxy)
            return self.getProxy()
